package com.usian.user.service.impl

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.usian.common.BaseServiceImpl
import com.usian.common.constants.user.UserConstants
import com.usian.model.article.pojos.ApAuthor
import com.usian.model.common.dtos.PageResponseResult
import com.usian.model.common.dtos.ResponseResult
import com.usian.model.media.pojos.WmUser
import com.usian.model.user.dtos.AuthDto
import com.usian.model.user.pojos.ApUserRealname
import com.usian.user.feign.ApAuthorFeign
import com.usian.user.feign.WmUserFeign
import com.usian.user.mapper.ApUserMapper
import com.usian.user.mapper.ApUserRealNameMapper
import com.usian.user.service.ApUserRealNameService
import io.seata.spring.annotation.GlobalTransactional
import org.apache.commons.beanutils.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

//kotlin中类可以被实例化，而接口不可以
@Service
open class ApUserRealNameServiceImpl : BaseServiceImpl<ApUserRealNameMapper, ApUserRealname>(), ApUserRealNameService {
    @Autowired
    private lateinit var apUserMapper: ApUserMapper

    @Autowired
    private lateinit var wmUserFeign: WmUserFeign

    @Autowired
    private lateinit var apAuthorFeign: ApAuthorFeign

    override fun loadListByStatus(dto: AuthDto): ResponseResult<*> {
        if (dto == null) return errorResult
        if (dto.status == null) dto.status = 1.toShort()
        val wrapper = QueryWrapper<ApUserRealname>()
        wrapper.eq("status", dto.status)
        val page = Page<ApUserRealname>(dto.page.toLong(), dto.size.toLong())
        val iPage = page(page, wrapper)
        val result = PageResponseResult(dto.page, dto.size, iPage.total.toInt())
        //getRecords()获取到列表数据
        result.data = iPage.records
        return result
    }

    @Transactional
    @GlobalTransactional  //分布式事务注解
    override fun updateStatusById(dto: AuthDto, status: Short): ResponseResult<*>? {
        if (dto == null || dto.id == null || status == null || status !== UserConstants.PASS_AUTH && status !== UserConstants.FAIL_AUTH) return errorResult
        updateById(ApUserRealname().apply {
            id = dto.id;
            this.status = status;
            reason = dto.msg ?: ""
        })
        if (status == UserConstants.PASS_AUTH) {
            val result: ResponseResult<Any>? = createWmAndAuthor(dto)
            if (result != null) return result
        }
        return sucResult
    }

    private fun createWmAndAuthor(dto: AuthDto): ResponseResult<Any>? {
        //通过认证id查询认证对象
        var userRealName = getById(dto.id)//认证对象
        //通过用户id查询用户对象
        var apUser = apUserMapper.selectById(userRealName.userId)//用户对象
        //根据用户名称查询自媒体用户
        var wmUser = wmUserFeign.findByName(apUser.name)
        if (wmUser == null) {
            //创建自媒体人
            wmUser = WmUser().apply {
                BeanUtils.copyProperties(this, apUser)
                createdTime = Date()
                status = UserConstants.PASS_AUTH.toInt()
                apUserId = apUser.id
            }
            wmUserFeign.save(wmUser)
        }
        //创建作者
        var author = apAuthorFeign.findByUserId(apUser.id)
        if (author == null) {
            apAuthorFeign.save(ApAuthor().apply {
                name = wmUser.name
                type = 2
                createdTime = Date()
                userId = apUser.id
                wmUserId = wmUser.id
            })
        }
        apUser.flag = 1//自媒体人
        apUserMapper.updateById(apUser)
        return null
    }
}
